var globalTimerAway = null;

function Me(user){
	this.inherit(user);

	global.createObject('me', this);

	this.room = global.getObject('room').id;
	this.session = globalUserSession;
	this.timezone = globalCurrentTimezone;

	this.messenger = new Messenger(this);
	this.com = new Communicator();
	this.requestPackageInterval = 3000;
	
	this.requestPackage();

	this.awayTimer;
	this.awayTime = 1000 * 60 * 3; // 3 minutes

	this.setAwayTimer();
	document.body.onmousemove = clearAway;
	document.body.onclick = clearAway;
	document.body.onkeydown = bodyKeyDown;
}

function bodyKeyDown(event){
	if(getKeyCode(event) == 27){
		if(globalShadingOpened) handleShading();
	}

	clearAway();	
}

Me.prototype.setAwayTimer = function(){
	if(this.awayTimer != null) clearTimeout(this.awayTimer);
	
	this.awayTimer = setTimeout(becomeAway,this.awayTime);
}

Me.prototype.becomeAway = function(){
	if(this.status != 2){ //if was away
		this.sendStatus(2);
		this.changeStatus(2);
	}
}

Me.prototype.clearAway = function(){
	if(this.status == 2){ //if was away
		this.sendStatus(1);
		this.changeStatus(1);
	}

	this.setAwayTimer();
}

Me.prototype.start = function(){
	this.room = global.getObject('room').id;
	this.messenger = new Messenger(this);
	this.com = new Communicator();
	this.requestPackageInterval = 5000;
	
	this.requestPackage();
}

Me.prototype.requestPackage = function(){
	var me = this;

	var timer = setTimeout(function(){
		me.com.resetParameters();
		me.com.addParameter('type','user');
		me.com.addParameter('task','getUpdates');
		me.com.addParameter('data',JSON.stringify({room: me.room}));
		me.com.sendRequest(receivePackage);
	}, this.requestPackageInterval);
}

Me.prototype.sendMessage = function(){
	this.messenger.send();
}

/* this.inherit(new ClassName) or this.inherit(ClassName) */
Me.prototype.inherit = function(Parent){
	var parentClass = (isObject(Parent)) ? Parent : new Parent;
	this.parentClass = new Array();
	
	for(var component in parentClass){
		var targetComponent = parentClass[component];
		var type = typeof targetComponent;
		
		if(type === 'function' || type === 'object' || type === 'number' || type === 'string'){
			if(component in this){ //do not allow overwriting
				this.parentClass[component] = targetComponent;
			}
			else this[component] = targetComponent;
		}
	}
}

Me.prototype.sendStatus = function(status){
	this.com.addParameter('type','user');
	this.com.addParameter('task','sendStatus');
	this.com.addParameter('data',JSON.stringify({room: this.room, status: status}));
	this.com.sendRequest();
}

function receivePackage(response){
	var me = global.getObject('me'); 
	//var obj = JSON.parse(replaceAll(response,'\\\\','\\')); 
	//response = response.split('\\\\').join('\\'); //(resp,'\\\\','\\');
	var obj = JSON.parse(response);
	var current;

	if(obj === 0) sessionEnded();
	else{
		for(var i=0, iMax=obj.length; i<iMax; i++){
			current = obj[i];
			//refatorar com chamada dinamica?!
			if(current[0] == 1){ //message
				receiveMessage(current[1]);
			}
			else if(current[0] == 2){ //access
				receiveAccess(current[1]);
			}
			else if(current[0] == 3){ //status
				receiveStatus(current[1]);
			}
		}
	}	

	me.requestPackage();
}

function sessionEnded(){
	alert('Sua sessão foi encerrada');
	
	backToIndex();
}

function receiveStatus(data){
	var userId = data[1];
	var status = data[0];

	var user = global.getObject('user', 'user_'+userId);
	user.changeStatus(status);
}

function receiveMessage(data){
	var message = new Message(data[0]);
	message.assignProperties(data[1],data[2],data[3],data[4],data[6]);
	message.place();
}

function startTyping(){
	var status = 3;
	var me = global.getObject('me');
	me.sendStatus(status);

	me.changeStatus(status);
}

function stopTyping(){
	globalTimerTyping = null;
	var status = 1;

	var me = global.getObject('me');
	me.sendStatus(status);
	me.changeStatus(status);
}

function clearAway(){
	global.getObject('me').clearAway();
}

function becomeAway(){
	global.getObject('me').becomeAway();
}